package com.gaofans.nrpc.common.serializer;

import com.gaofans.nrpc.common.serializer.jdk.JdkSerializerBuilder;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author GaoFans
 */
public class SerializerFactory {

    private final Map<Byte,SerializerBuilder> serializerBuilderMap;

    public SerializerFactory() {
        this.serializerBuilderMap = new ConcurrentHashMap<>();
        serializerBuilderMap.put((byte) 0,new JdkSerializerBuilder());
    }

    public void register(byte type,SerializerBuilder builder){
        serializerBuilderMap.put(type,Objects.requireNonNull(builder));
    }

    public SerializerBuilder remove(byte type){
        return serializerBuilderMap.remove(type);
    }

    public Serializer getSerializer(byte type){
        return Objects.requireNonNull(serializerBuilderMap.get(type)).build();
    }
}
